/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkgMetodoPyramid.pkgReporte;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import pkgMetodoPyramid.pkgNegocio.EntityPyramid;
import pkgMetodoPyramid.pkgSoporte.Constants;
import pkgUtil.BuilderReport;
import pkgUtil.Util;

/**
 *
 * @author Administrador
 */
public abstract class GestorGraphPyramid {

    /*
     * private Integer totalVarones; private Integer totalMujeres; private
     * Integer total; private String titulo; private String subTitulo; private
     * ReqRespPyramid reqRespPyramid;
     */
    private BDataRptChart dataRptChart;
    private List<BRptGraph> lstBRptGraph;
    private JFreeChart chartVarones;
    private JFreeChart chartMujeres;

    private enum ParametrosReporte {

        P_TITULO {

            @Override
            public String toString() {
                return "pTitulo";
            }
        },
        P_SUB_TITULO {

            @Override
            public String toString() {
                return "pSubTitulo";
            }
        },
        P_TOTAL {

            @Override
            public String toString() {
                return "pTotal";
            }
        },
        P_TOTAL_VARONES {

            @Override
            public String toString() {
                return "pTotalVarones";
            }
        },
        P_TOTAL_MUJERES {

            @Override
            public String toString() {
                return "pTotalMujeres";
            }
        },
        p_PATH_LOGO {

            @Override
            public String toString() {
                return "p_PathLogo";
            }
        },
        P_GRAFICO_LOGO {

            @Override
            public String toString() {
                return "p_Grafico_Logo";
            }
        },
        P_GRAFICO_VARONES {

            @Override
            public String toString() {
                return "p_Grafico_Varones";
            }
        },
        P_GRAFICO_MUJERES {

            @Override
            public String toString() {
                return "p_Grafico_Mujeres";
            }
        }
    }

    /**
     * @return the dataRptChart
     */
    public BDataRptChart getDataRptChart() {
        return dataRptChart;
    }

    /**
     * @param dataRptChart the dataRptChart to set
     */
    public void setDataRptChart(BDataRptChart dataRptChart) {
        this.dataRptChart = dataRptChart;
    }

    protected enum SeriesGrafico {

        P_SERIE_VARONES {

            @Override
            public String toString() {
                return "Varones";
            }
        },
        P_SERIE_MUJERES {

            @Override
            public String toString() {
                return "Mujeres";
            }
        }
    }

    public GestorGraphPyramid() {
    }

    /**
     * @return the chartVarones
     */
    protected JFreeChart getChartVarones() {
        return chartVarones;
    }

    /**
     * @param chartVarones the chartVarones to set
     */
    protected void setChartVarones(JFreeChart chartVarones) {
        this.chartVarones = chartVarones;
    }

    /**
     * @return the chartMujeres
     */
    protected JFreeChart getChartMujeres() {
        return chartMujeres;
    }

    /**
     * @param chartMujeres the chartMujeres to set
     */
    protected void setChartMujeres(JFreeChart chartMujeres) {
        this.chartMujeres = chartMujeres;
    }

    protected void setRange(JFreeChart chart) {
        CategoryPlot plot = chart.getCategoryPlot();
        plot.getRangeAxis().setRange(getMinValueInterval(), getMaxValueInterval());
    }

    protected List getListBean() throws ProcessReportException {
        if (dataRptChart.getReqRespPyramid() == null || !dataRptChart.getReqRespPyramid().poseeEntity()) {
            throw new ProcessReportException("No existen EntityPyramid para generar el reporte");
        }
        if (lstBRptGraph != null) {
            return lstBRptGraph;
        }
        lstBRptGraph = new ArrayList();
        Iterator it = dataRptChart.getReqRespPyramid().getIterator();
        while (it.hasNext()) {
            EntityPyramid entity = (EntityPyramid) it.next();

            BRptGraph bean = new BRptGraph();
            bean.setRango(entity.getEdad());
            bean.setCantidadVarones(entity.getVarones());
            bean.setCantidadMujeres(entity.getMujeres());
            bean.setPorc_varones(Util.redondear(entity.getPorc_varones(), Constants.CANTIDAD_DECIMALES));
            bean.setPorc_mujeres(Util.redondear((entity.getPorc_mujers()), Constants.CANTIDAD_DECIMALES));
            bean.setPorc_vtotal(Util.redondear(entity.getPorc_vtotal(), Constants.CANTIDAD_DECIMALES));
            bean.setPorc_mtotal(Util.redondear(entity.getPorc_mtotal(), Constants.CANTIDAD_DECIMALES));

            lstBRptGraph.add(bean);
        }
        return lstBRptGraph;

    }

    protected void personalizarChartVarones(JFreeChart chart) {

        CategoryPlot plot = chart.getCategoryPlot();

        plot.setBackgroundPaint(new Color(0xEE, 0xEE, 0xFF));
        plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        plot.getRangeAxis().setInverted(true);
        plot.getDomainAxis().setLabelAngle(Constants.ANGULO_DOMAINAXIS_VARONES);
        BarRenderer render = (BarRenderer) plot.getRenderer();
        render.setSeriesPaint(0, Color.BLUE);

        personalizarFuente(chart);
        setRange(chart);
    }

    protected void personalizarChartMujeres(JFreeChart chart) {

        CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(new Color(0xEE, 0xEE, 0xFF));
        plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        plot.getDomainAxis().setLabelAngle(Constants.ANGULO_DOMAINAXIS_MUJERES);
        personalizarFuente(chart);

        setRange(chart);
    }

    private void personalizarFuente(JFreeChart chart) {
        Font fTitulo = new Font(Constants.FONT_TITULO_CHART, Font.BOLD, Constants.SIZE_TITULO_CHART);
        Font fAxis = new Font(Constants.FONT_TITULO_CHART, Font.BOLD, Constants.SIZE_TITULO_CHART);
        chart.getTitle().setFont(fTitulo);
        CategoryPlot plot = chart.getCategoryPlot();
        plot.getDomainAxis().setLabelFont(fAxis);
        plot.getRangeAxis().setLabelFont(fAxis);
    }

    protected BufferedImage getImageGrafVarones() {
        BufferedImage image_Grafico_V = getChartVarones().createBufferedImage(Constants.RPT_IMAGECHAR_WIDTH, Constants.RPT_IMAGECHAR_HEIGHT);
        return image_Grafico_V;
    }

    protected BufferedImage getImageGrafMujeres() {
        BufferedImage image_Grafico_M = getChartMujeres().createBufferedImage(Constants.RPT_IMAGECHAR_WIDTH, Constants.RPT_IMAGECHAR_HEIGHT);
        return image_Grafico_M;
    }

    protected JFreeChart createChart(CategoryDataset dataset, String titulo, String nomAxisCategoria, String nomAxisValor) {

        JFreeChart chart = ChartFactory.createBarChart(
                titulo, // chart title
                nomAxisCategoria, // domain axis label
                nomAxisValor, // range axis label
                dataset, // data
                PlotOrientation.HORIZONTAL, // orientation
                true, // include legend
                true, // tooltips?
                false // URLs?
                );
        return chart;
    }

    protected void createReporte(String pathFileJasper) throws JRException, ProcessReportException {
        Map parametros = new HashMap();
        parametros.put(ParametrosReporte.P_TOTAL_VARONES.toString(), getDataRptChart().getTotalVarones());
        parametros.put(ParametrosReporte.P_TOTAL_MUJERES.toString(), getDataRptChart().getTotalMujeres());
        parametros.put(ParametrosReporte.P_TOTAL.toString(), getDataRptChart().getTotal());
        parametros.put(ParametrosReporte.P_TITULO.toString(), getDataRptChart().getTitulo());
        parametros.put(ParametrosReporte.P_SUB_TITULO.toString(), getDataRptChart().getSubTitulo());
        parametros.put(ParametrosReporte.P_GRAFICO_LOGO.toString(), getImageLogo());
        parametros.put(ParametrosReporte.P_GRAFICO_VARONES.toString(), getImageGrafVarones());
        parametros.put(ParametrosReporte.P_GRAFICO_MUJERES.toString(), getImageGrafMujeres());
        
        
        BuilderReport builderReport = new BuilderReport();
        builderReport.setPathFileJasper(pathFileJasper);
        builderReport.setHmParameters(parametros);
        builderReport.setCollectionDetail(getListBean());
        builderReport.buildAndShowReport();

    }

    private BufferedImage getImageLogo() {

        try {
            InputStream is = getClass().getResourceAsStream(Constants.PATH_LOGO_GRAPH);
            BufferedImage buffImage = ImageIO.read(is);
            
            return buffImage;
        } catch (Exception ex) {
            return null;
        }
    }

    public abstract void generarReporte() throws ProcessReportException, JRException;

    public abstract void generarGrafico() throws ProcessGraphException, JRException;

    public abstract double getMaxValueInterval();

    public abstract double getMinValueInterval();
}
